//
//  ViewController.m
//  PlistTest
//
//  Created by 杨康 on 2018/2/13.
//  Copyright © 2018年 杨康. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    
}
-(void)readFile
{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"];
    //1
    //    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    //2
    NSMutableArray *arr = [[NSMutableArray alloc]initWithContentsOfFile:plistPath];
}
-(void)writeFile
{
    //plist文件已经存在(在工厂目录中创建）
    //获取已有plist文件的路径
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"];
    //plist 中的 root是 NSArray结构,NSArray里面是NSDictionary结构:
    NSMutableArray *usersArr = [[NSMutableArray alloc]initWithContentsOfFile:plistPath];
    
    NSDictionary *dic =@{@"name":@"张三",@"age":@"20"};
    [usersArr addObject:dic];
    //把数据写入plist文件
    BOOL ifSave = [[usersArr copy] writeToFile:plistPath atomically:YES];
    //保存成功
    if (ifSave) {
    }
}
-(void)noautoWrite
{
   //通过代码创建plist
    //获取本地沙盒路径
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //获取完整路径
    NSString *documentsPath = [path objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"usersList.plist"];
    NSMutableDictionary *usersDic = [[NSMutableDictionary alloc ] init];
    //设置属性值
    [usersDic setObject:@"chan" forKey:@"name"];
    [usersDic setObject:@"123456" forKey:@"password"];
    //写入文件
    [usersDic writeToFile:plistPath atomically:YES];
}


@end
